package redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.*;

public class RdeisDemoHash {
    public static void test1(Jedis jedis){
        System.out.println("hset  hget hexists hdel");
        jedis.flushAll();

        jedis.hset("key","f1","111");
        Map<String,String> fields = new HashMap<>();
        fields.put("f2","222");
        fields.put("f3","333");
        fields.put("f5","555");
        jedis.hset("key",fields);

        String ret = jedis.hget("key","f1");
        System.out.println("f1:"+ret);

        ret = jedis.hget("key","f2");
        System.out.println("f2:"+ret);
        ret = jedis.hget("key","f5");
        System.out.println("f5:"+ret);

        boolean rest = jedis.hexists("key","f1");
        System.out.println("has f1:"+rest);

        rest = jedis.hexists("key","f7");
        System.out.println("no f7:"+rest);

        long delnums = jedis.hdel("key","f1","111");
        System.out.println("hdel nums:"+delnums);
        rest = jedis.hexists("key","f1");
        System.out.println("after hdel ,no f1:"+rest);

    }

    public static void test2(Jedis jedis){
        System.out.println("hkeys hvals");
        jedis.flushAll();

        jedis.hset("key","f1","111");
        jedis.hset("key","f2","222");
        jedis.hset("key","f3","333");
        jedis.hset("key","f6","666");
        Set<String> fields =jedis.hkeys("key");
        System.out.println("key's fields:"+fields);
        List<String> vals = jedis.hvals("key");
        System.out.println("key's vals"+ vals);

    }

    public static void test3(Jedis jedis){
        System.out.println("hmset  hmget");
        jedis.flushAll();

        Map<String,String> map = new HashMap<>();
        map.put("f1","111");
        map.put("f2","222");
        map.put("f3","333");
        jedis.hmset("key",map);

        List<String> ret = jedis.hmget("key","f1","f2","f3");
        System.out.println("f1 f2 f3 :"+ret);
    }

    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");

        try(Jedis jedis = jedisPool.getResource()) {

            //test1(jedis);
            //test2(jedis);
            test3(jedis);

        }
    }
}
